package com.gzwanchuang.xhgj.ui.page.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.data.repository.Apis;
import com.gzwanchuang.xhgj.bridge.request.MineRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.mine.JieShuanMoViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentJieSuanMoBinding;
import com.gzwanchuang.xhgj.help.DialogUtils;
import com.gzwanchuang.xhgj.help.UserHelper;
import com.gzwanchuang.xhgj.net.NetworkManager;
import com.gzwanchuang.xhgj.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.xhgj.proto.Mergedintoaccount;
import com.gzwanchuang.xhgj.ui.LoginActivity;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;
import com.google.protobuf.InvalidProtocolBufferException;
import com.gzwanchuang.xhgj.ui.utils.CountdownUtils;

import kale.ui.view.dialog.EasyDialog;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class JieShuanMoFragment extends BaseFragment {
    private FragmentJieSuanMoBinding mBinding;
    private JieShuanMoViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;

    private CountdownUtils countdownUtils;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(JieShuanMoViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jie_suan_mo, container, false);
        mBinding = FragmentJieSuanMoBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            showShortToast(s);
        });

        mRequestViewModel.getResultString().observe(this, s -> {
            showShortToast(s);
            nav().navigateUp();
        });


        countdownUtils = new CountdownUtils(mBinding.tvGetCode, new CountdownUtils.OnFinishListener() {
            @Override
            public void finish() {
                mViewModel.changeIsEnable.set(true);
            }
        });

        NetworkManager.INSTANCE.post(Apis.pattern_query, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Mergedintoaccount.pattern_query pattern_query = Mergedintoaccount.pattern_query.parseFrom(response);
                if (pattern_query.getType().equals("SELF")) {
                    mViewModel.changeType.set("修改为自动结算");
                    mViewModel.type.set("自助结算");
                } else {
                    mViewModel.changeType.set("修改为自助结算");
                    mViewModel.type.set("自动结算");
                }

                if (pattern_query.getModify().equals("1")) {
                    mBinding.submitBtn.setEnabled(true);
                   mViewModel.changeIsEnable.set(true);
                } else {
                    mBinding.submitBtn.setEnabled(false);
                    mViewModel.changeIsEnable.set(false);
                    mBinding.submitBtn.setText("暂不可修改");
                }

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
            }
        });
    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void changeType()
        {
            DialogUtils.showDefaultDialog(mActivity, "温馨提示", "修改为自动结算后，系统将按照当日结算规则出款！系统将在1小时内完成更新结算模式！", new DialogUtils.DialogImpl() {
                @Override
                public void onOk() {
                    showProgress();
                    NetworkManager.INSTANCE.post(Apis.pattern_send, new OnRequestCallBack() {
                        @Override
                        public void onOk(byte[] response) {
                        }

                        @Override
                        public void onError(int errorCode, String errorMessage) {
                            hideProgress();
                            showShortToast(errorMessage);
                            if (errorCode==200)
                            {
                                mViewModel.isCode.set(true);
                                mViewModel.changeIsEnable.set(false);
                                countdownUtils.RunTimer();
                            }
                        }
                    });
                }
            });





        }







        public void submit() {
            Mergedintoaccount.pattern_modify.Builder builder = Mergedintoaccount.pattern_modify.newBuilder();
            builder.setCaptcha(mViewModel.code.get());
            if (mViewModel.type.get().equals("自助结算")) {
                builder.setType("D0");
            } else {
                builder.setType("SELF");
            }
            NetworkManager.INSTANCE.post(Apis.pattern_modify, builder.build().toByteArray(), new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) {
                    showShortToast("修改成功");
                    mSharedViewModel.hexiaoType.setValue(true);
                    mSharedViewModel.isCheck.setValue(true);
                    nav().navigateUp();
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
                    showShortToast(errorMessage);
                }
            });
        }
    }
}
